import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'

export default defineConfig(({ command, mode }) => {
  // 获取配置的环境变量
  const env = loadEnv(mode, process.cwd())

  return {
    base: './',
    plugins: [vue()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      proxy: {
        '/devapi': {
          target: 'http://192.168.8.20:8051', //公司
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/devapi/, '/bdmap_server'),
          bypass(req, res, options) {
            const realUrl = options.target + (options.rewrite ? options.rewrite(req.url) : '')
            res.setHeader('A-Real-Url', realUrl) // 添加响应标头(A-Real-Url为自定义命名)，在接口response headers 可以看到真实请求地址
          }
        }
      }
    },
  }
})
